//
//  ClassModel.m
//  testWebView
//
//  Created by ty804 on 02/06/2019.
//  Copyright © 2019 ty804. All rights reserved.
//
#define MIX_FUNCTION_NAME  @"function_"
#define MIX_PRAMETER_NAME  @"prameter_"
#import "ClassModel.h"
#import "Objc_Name.h"
@implementation ClassModel
-(void)setClassCollection:(NSArray *)classCollection{
    _classCollection=classCollection;
    
}
-(void)setClassType:(NSString *)classType{
    _classType=classType;
    
}
-(void)setClassSouce:(NSString *)classSouce{
    _classSouce=classSouce;
    
}
-(void)setClassFunctionName:(NSString *)classFunctionName{
    _classFunctionName=classFunctionName;

}
-(void)setClassIntPutSouce:(NSString *)classIntPutSouce{
    _classIntPutSouce=classIntPutSouce;
   
}
-(void)setDefautInfo{
    self.classCollection=@[@"UIView",
                           @"UITextField",
                           @"UILabel",
                           @"UIButton",
                           @"UITextView",
//                           @"UICollectionView",
                           @"UITableView"];
    self.classType=self.classCollection[arc4random() % [self.classCollection count]];
    NSString *path = [[NSBundle mainBundle] pathForResource:self.classType ofType:@"txt"];
    NSString *content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    //替换function变量
    NSRange rangeFunction= [content rangeOfString:MIX_FUNCTION_NAME];
    if (rangeFunction.location == NSNotFound)
    {
        NSLog(@"没有需要替换的PRAMETER变量");
    }else {
        for (int i=100; i>=0; i--) {
            NSString *strReplace=[self mix_name:MIX_FUNCTION_NAME nun:i];
            NSString *str=[Objc_Name arc4random_function_name];
            content=[content stringByReplacingOccurrencesOfString:strReplace withString:str];
            }
    }
    
    //替换Prameter变量
    NSRange rangePrameter = [content rangeOfString:MIX_PRAMETER_NAME];
    if (rangePrameter.location == NSNotFound)
    {
        NSLog(@"没有需要替换的Prameter变量");
    }else {
        for (int i=100; i>=0; i--) {
            NSString *strReplace=[self mix_name:MIX_PRAMETER_NAME nun:i];
            NSString *str=[Objc_Name arc4random_prameter_name];
            content=[content stringByReplacingOccurrencesOfString:strReplace withString:str];
        }
    }
    
    
    NSString *str=[Objc_Name arc4random_class_name];
    content=[content stringByReplacingOccurrencesOfString:@"class_1" withString:str];
    
    NSArray *arrContent=[content componentsSeparatedByString:@"\n"];
    NSMutableArray *arrCopy=[NSMutableArray arrayWithArray:arrContent];
    [arrContent enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj localizedStandardContainsString:@"placeholderName"] ) {
            NSString *str_replace=[Objc_Name arc4random_prameter_name];
           obj= [obj stringByReplacingOccurrencesOfString:@"placeholderName" withString:str_replace];
            [arrCopy replaceObjectAtIndex:idx withObject:obj];
        }
    }];
    content=[arrCopy componentsJoinedByString:@"\n"];
    
    
    self.className=str;
    self.classSouce=content;
    NSArray *arr=@[@"0",@"1"];
    NSString *strType=arr[arc4random() % [arr count]];
    if (strType.boolValue) {
        self.classFunctionName=[Objc_Name arc4random_prameter_name];
        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"OutPut" ofType:@"txt"];
        NSString *content1 = [[NSString alloc] initWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
        content1=[content1 stringByReplacingOccurrencesOfString:@"functionName" withString:self.classFunctionName];
        content1=[content1 stringByReplacingOccurrencesOfString:@"className" withString:self.className];
        self.classIntPutSouce=content1;
        
        NSString *strPro=[NSString stringWithFormat:@"@property (nonatomic,strong)%@ *%@;",self.className,self.self.classFunctionName];
        self.classPropertyName=strPro;
       
    }else{
        self.classFunctionName=@"";
        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"OutPutLocal" ofType:@"txt"];
        NSString *content1 = [[NSString alloc] initWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
        NSString *strPam=[Objc_Name arc4random_prameter_name];
        content1=[content1 stringByReplacingOccurrencesOfString:@"className" withString:self.className];
        content1=[content1 stringByReplacingOccurrencesOfString:@"functionName" withString:strPam];
        self.classIntPutSouce=content1;
        self.classPropertyName=@"";
        
    }
    
}
-(NSString *)mix_name:(NSString *)type nun:(int)num_int{
    
    return [NSString stringWithFormat:@"%@%d",type,num_int];
}
-(NSString *)codeByOutPut{
    if ([self.classFunctionName isEqualToString:@""]) {
        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"OutPutLocal" ofType:@"txt"];
        NSString *content1 = [[NSString alloc] initWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
        NSString *strPam=[Objc_Name arc4random_prameter_name];
        content1=[content1 stringByReplacingOccurrencesOfString:@"className" withString:self.className];
        content1=[content1 stringByReplacingOccurrencesOfString:@"functionName" withString:strPam];
        self.classIntPutSouce=content1;
        self.classPropertyName=@"";
        
    }else{
        self.classFunctionName=[Objc_Name arc4random_prameter_name];
        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"OutPut" ofType:@"txt"];
        NSString *content1 = [[NSString alloc] initWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
        content1=[content1 stringByReplacingOccurrencesOfString:@"functionName" withString:self.classFunctionName];
        content1=[content1 stringByReplacingOccurrencesOfString:@"className" withString:self.className];
        self.classIntPutSouce=content1;
        
        NSString *strPro=[NSString stringWithFormat:@"@property (nonatomic,strong)%@ *%@;",self.className,self.self.classFunctionName];
        self.classPropertyName=strPro;
        
    }
    return self.classIntPutSouce;
}
@end
